/*
 * Created on Mar 19, 2007
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.dps.dao.channel;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import com.broadvision.basic.BVException;
import com.broadvision.data.client.Category;
import com.broadvision.data.client.DataManager;
import com.broadvision.data.common.ContentKey;
import com.broadvision.data.common.DataConstants;
import com.broadvision.data.common.TableData;
import com.broadvision.data.exceptions.CategoryNotFoundException;
import com.broadvision.data.exceptions.DataDeadLockedException;
import com.broadvision.data.exceptions.DataException;
import com.broadvision.portal.services.Channel;
import com.broadvision.portal.services.PortalContentKeyNotFoundException;
import com.broadvision.portal.services.PortalDataException;
import com.broadvision.portal.services.PortalVisitor;
import com.broadvision.visitor.exceptions.VisitorException;
import com.broadvision.web.servlet.SessionVisitor;

/**
 * @author hassan.naqvi
 * @edited August 03, 2008
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ChannelDao {
	

	
	public ChannelDao(SessionVisitor sessionVisitor){
		serviceId=sessionVisitor.getServiceId();
		try {
			portalVisitor=sessionVisitor.getPortalVisitor();
		} catch (VisitorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public ChannelDao(){}
	
	
	public List getSubChannels(String parentChannelId)
	{
		
		System.out.println("Entering into getSubChannels() of class: "+this.getClass());
		List channels = new ArrayList();

		try {
			Channel parentChannel = new Channel(serviceId,new Long(parentChannelId).longValue(),portalVisitor);
			String menuPath=parentChannel.getPathName();
			ContentKey contentKey = new ContentKey(serviceId, menuPath, "KM_PROGRAM");
			Category category;
			category = dataManager.getCategory(contentKey);
			Channel channel2 = Channel.getChannel(serviceId, category.getOid(), portalVisitor);
			List subChannels = channel2.getChildChannels(DataConstants.BV_ONLINE_DB_CONTENT);

			for (int j = 0; j < subChannels.size(); j++) {
		         Channel subChannel = (Channel)subChannels.get(j);
		         Channels channel = new Channels();
				 channel.setChannelID(new Long(subChannel.getId()).toString());
				 channel.setChannelName(subChannel.getName());
			     String selectedChannels=getSelectedChannels(parentChannelId);
			     channel.setSelectedChannels(selectedChannels);
			     channels.add(channel);
	      }
		}  catch (DataDeadLockedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (CategoryNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (DataException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (PortalContentKeyNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PortalDataException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (BVException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		System.out.println("returning channels from getSubChannels()......");	
		return channels;
	}
	
	public List getRootChannels(){
		TableData resTblData = null;
        String query = "SELECT OID FROM BVUSER.BV_CATEGORY WHERE Name='"+NAVIGATION_ROOT+"' AND (STORE_ID='1003')";
	    try {
			resTblData = getDataByQuery(query);
		} catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        String navId=resTblData.getStringValue(0,0);		
		return getSubChannels(navId);
	}
	
	
	 /**
	    * Returns a list of data rows in the form of a table.
	    * Returned data will have no content type information in the
	    * return results.
	    * 
	    * @param selectQuery query statement
	    * @return TableData object
	    * @throws DataException if an error occurs. Also logs the
	    * error message and sql query that failed.
	    */
	   public TableData getDataByQuery(String selectQuery) throws DataException
	   {
		  dataManager = new DataManager(); 
	      TableData resTblData;
	      try {
	         resTblData = dataManager.getDataByQuery(null, selectQuery);
	      } catch (DataException de) {
	         System.out.println(de.toString());
	         throw de;
	      }

	      return resTblData;
	   }

	   String getSelectedChannels(String channelId){
		   System.out.println("Entering into getSelectedChannels() of class: "+this.getClass());
		   String selectedChannels=""; 
		   TableData resTblData = null;
	
	  try {
		   Channel channel = new Channel(serviceId, new Long(channelId).longValue(),portalVisitor);
		   String menuPath=channel.getPathName();
		   String[] tempArray = menuPath.split("/");
		   int count=0;
		   for (int x=0; x<tempArray.length; x++){
		    	 System.out.println("split---"+tempArray[x]);
		    	 if(tempArray[x].equalsIgnoreCase(NAVIGATION_ROOT)){
		           count=x;
		           break;
		    	 } 
		     }
		    String[] result=new String[(tempArray.length)-count];
		    count++;

		    if(!(count==tempArray.length)){
			    for(int i=0; i<result.length;i++,count++){
		            result[i]=tempArray[count-1];
			        String query = "SELECT OID FROM BVUSER.BV_CATEGORY WHERE Name='"+result[i]+"' AND (STORE_ID='1003')";
				    resTblData = getDataByQuery(query);
			        String str=resTblData.getStringValue(0,0);
			        selectedChannels+=str+",";
			     }   
		    }else{
	    	   selectedChannels=channelId;
	    	   return selectedChannels;
	         }

		    if(selectedChannels.endsWith(",")){
		    	int length=selectedChannels.length();
		    	selectedChannels=selectedChannels.substring(0, length-1);
	         }		

		    return selectedChannels;  
	  
	   
	} catch (DataException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (PortalContentKeyNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NumberFormatException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (PortalDataException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		  return selectedChannels; 
  }

    private static ResourceBundle rb = ResourceBundle.getBundle("Connection");   
	public static DataManager dataManager = null;
	private  static int serviceId =0;
	private  static PortalVisitor portalVisitor= null;
	private  static String NAVIGATION_ROOT =rb.getString("navigation.root");
	
}
